﻿using AutoMapper;
using HelenServer.FileStorage.Contracts;
using HelenServer.Grpc.Contracts;

namespace HelenServer.FileStorage.Services;

internal class FSDownloadFile : GrpcMethod<int, GrpcResult<DownloadGrpcModel>>, IFSDownloadFile
{
    private readonly IFileStorageService _service;
    private readonly IMapper _mapper;

    public FSDownloadFile(IFileStorageService fileStorage, IMapper mapper)
    {
        _service = fileStorage;
        _mapper = mapper;
    }

    public override async Task<GrpcResult<DownloadGrpcModel>> InvokeAsync(int request, ServerCallContext context)
    {
        var operation = context.GetOperation(request);

        var result = await _service.DownloadFileAsync(operation);

        return new GrpcResult<DownloadGrpcModel>(
            result.Status,
            result.Message,
            result.FailedHistory,
            _mapper.Map<DownloadModel, DownloadGrpcModel>(result.Data));
    }
}
